package chabernac.gravidroid.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import android.graphics.Color;
import chabernac.util.Iterables;
import chabernac.util.iPredicate;

public class ParticleInteraction {
	private final GravitySimulator mySimlator;
	private final iPredicate<iMass> myMassPredicate = new MassPredicate();
	private final Random myRandom = new Random();

	public ParticleInteraction(GravitySimulator mySimlator) {
		super();
		this.mySimlator = mySimlator;
	}

	public void iterate(){
//		Iterable<iMass> theMasses = Iterables.filter(mySimlator.getMasses(), myMassPredicate);
//
//		Iterator<iMass> theIterator = theMasses.iterator();
//
//		Set<Mass> theProcessedMasses = new HashSet<Mass>();
//
//		while(theIterator.hasNext()){
//			Mass theMass = (Mass)theIterator.next();
//
//			if(!theProcessedMasses.contains(theMass)){
//				Set<Mass> theCollidingMasses = new HashSet<Mass>();
//				theMass.getAllCollodingMassesOfSameColor(theCollidingMasses);
//
//				processCollidingMasses(theCollidingMasses);
//
//				theProcessedMasses.addAll(theCollidingMasses);
//			}
//		}

		/*
		theProcessedMasses.clear();

		theIterator = theMasses.iterator();
		while(theIterator.hasNext()){
			Mass theMass = (Mass)theIterator.next();
			if(!theProcessedMasses.contains(theMass)){
				for(Mass theCollidingMass: theMass.getCollidingMasses()){
					if(theCollidingMass.color != theMass.color){
						//these two masses have different color let them explode

//						for(int i=0;i<2;i++){
							mySimlator.addMass( new Mass()
							.setLocation( theMass.location)
							.setMass( 2 )
							.setRadius( 100 )
							.setCoreRadius(100)
							.setColor(getColor(myRandom.nextInt(2)))
							.setSpeed( new Vector(myRandom.nextInt() % 500,myRandom.nextInt()  % 500) ) );
							
							Vector theLocation = new Vector(theCollidingMass.location);
							theLocation.x += 200;
							theLocation.y += 200;
							
							mySimlator.addMass( new Mass()
							.setLocation(theLocation)
							.setMass( 2 )
							.setRadius( 100 )
							.setCoreRadius(100)
							.setColor(getColor(myRandom.nextInt(2)))
							.setSpeed( new Vector(myRandom.nextInt() % 500,myRandom.nextInt()  % 500) ) );
//						}
						mySimlator.removeMass(theCollidingMass);
						mySimlator.removeMass(theMass);
					}
					theProcessedMasses.add(theCollidingMass);
				}
				theProcessedMasses.add(theMass);
			}
		}
		*/
	}

	private int getColor(int anIndex){
		if(anIndex == 0) return Color.RED;
		if(anIndex == 1) return Color.BLUE;
		if(anIndex == 2) return Color.GREEN;
		if(anIndex == 3) return Color.MAGENTA;
		return Color.WHITE;
	}

	private void processCollidingMasses(Set<Mass> aCollidingMasses) {
		Map<Integer, List<Mass>> theMassesPerColor = new HashMap<Integer,List<Mass>>();

		for(Mass theMass : aCollidingMasses){
			if(!theMassesPerColor.containsKey(theMass.color)){
				theMassesPerColor.put(theMass.color, new ArrayList<Mass>());
			}

			theMassesPerColor.get(theMass.color).add(theMass);
		}

		for(List<Mass> theMasses : theMassesPerColor.values()){
			if(theMasses.size() >= 5){
				//remove all the masses from the simulator
				mySimlator.removeMasses(theMasses);
			}
		}
	}

}
